<?php
namespace app\index\service;
use think\Db;
use think\Cache;
use think\Loader;
class  Other{


    //首页查询顾问卡
    function counselorList(){
         $list = Db::name('counselor')->field('ck_name,ck_price,ck_ps,counselor_id')->order('ck_sort desc,ck_addtime desc')->select();
         $column       = array();
         if(!empty($list)){
             foreach($list as $k=>$v){
                $column[$k]['name'] = $v['ck_name'];
                $ps_list = explode('&',$v['ck_ps']);
                foreach($ps_list as $k1=>$vo){
                    $ps_list_data = explode(',',$vo);
                    $list[$k]['ps_list'][$k1]['ps_a'] = $ps_list_data[0];
                    $list[$k]['ps_list'][$k1]['ps_b'] = $ps_list_data[1];
                    $list[$k]['ps_list'][$k1]['ps_c'] = $ps_list_data[2];
                }
                unset($v['ck_ps']);
             }
         }
        $data['list']   = $list;
        $data['column'] = $column;
        return $data;
    }


    //查询顾问卡
    function counselorBuy(){
        $list = Db::name('counselor')->field('ck_name,ck_price,ck_ps,counselor_id')->order('ck_sort desc,ck_addtime desc')->select();
        if(!empty($list)){
            foreach($list as $k=>$v){
                $ps_list = explode('&',$v['ck_ps']);
                foreach($ps_list as $k1=>$vo){
                    $ps_list_data = explode(',',$vo);
                    $list[$k]['ps_list'][$k1]['ps_a'] = $ps_list_data[0];
                    $list[$k]['ps_list'][$k1]['ps_b'] = $ps_list_data[1];
                    $list[$k]['ps_list'][$k1]['ps_c'] = $ps_list_data[2];
                }

            }
        }
        return $list;
    }


    //律师列表
    function lawyerList(){
        $paginate = 15;
        $where['l_state'] = 2;
        $info  = Db::name('lawyer')->where($where)->field('l_name,l_office,l_age,l_site,l_field,l_describe,l_img,lawyer_id')->order('l_sort desc,l_addtime desc')->paginate($paginate);
        $page  = $info->render();
        $list  = $info->toArray();
        foreach($list['data'] as $k=>$v){
            $list['data'][$k]['list'] = explode(',',$v['l_field']);
        }
        $data['list'] = $list['data'];
        $data['page'] = $page;
        return $data;
    }

    //律师详情
    function lawyerFind(){
        $lid  = input('lid');
        $data = Db::name('lawyer')->where('lawyer_id',$lid)->find();
        $data['list'] = explode(',',$data['l_field']);
        return $data;
    }

    //首页显示数据
    function indexData(){
        //律师推荐
        $where['l_state'] = 2;
        $data['lawyer_list'] = Db::name('lawyer')->where($where)->field('l_name,l_office,l_age,l_site,l_field,l_describe,l_img,lawyer_id')->order('l_sort desc,l_addtime desc')->limit(4)->select();
        //咨讯
/*        $where_a['n_state'] = 2;
        $join  = [
            ['lt_news_type b','a.news_type_id=b.news_type_id','LEFT'],
        ];
        $field = 'a.n_title,a.news_id,a.n_describe,a.n_img,a.n_newstime,b.nt_name';
        $data['news_list']  = Db::name('news')->alias('a')->join($join)->where($where_a)->field($field)->order('a.n_newstime desc')->limit(3)->select();*/
        return $data;
    }

    //留言
    function solutionPresentApi($input){
        $va_phone = preg_match('/^1\d{10}$/', $input['phone']);
        if (!$va_phone) return messageInfo('101', '手机号码格式错误！');
        if (empty($input['username'])) return messageInfo('101', '姓名必需填写!');
        if (empty($input['content'])) return messageInfo('101', '咨询问题不能为空!');
        $ip    = get_client_ip_c();
        $count = Cache::get($ip);
        if($count > 10) return messageInfo('101', '你当天留言次数已用完，请隔天在来留言!');
        //查询随机后台管理员
        $join  = [
            ['lt_admin b','a.admin_id=b.admin_id','LEFT'],
        ];
        $admin  = Db::name('push_sie')->alias('a')->join($join)->where('push_type',$input['type'])->order('rand()')->field('b.a_phone,a.admin_id')->find();
        $date   = date('Y-m-d H:i:s');
        $data_array  = [
            'leave_type' => $input['type'],
            'username' => $input['username'],
            'phone' => $input['phone'],
            'content' => $input['content'],
            'leave_ip' => $ip,
            'admin_id' => $admin['admin_id']?$admin['admin_id']:0,
            'leave_addtime' => date('Y-m-d H:i:s'),
        ];

        if(array_key_exists('source',$input)){
            $data_array['source'] = $input['source'];
        }
        Cache::inc($ip);
        $a = Db::name('leave')->insert($data_array);
        if(!$a)  return messageInfo ( '101', '留言失败！' );
        if(!empty($admin['a_phone'])){//给后台发送通知
            Loader::import('Aldy.Sms', EXTEND_PATH, '.class.php');
            $cofig ['templateCode'] = config('AlidayuID_TZ');
            $sms = new \Sms($cofig);
            $smsParams = array ('name' => $admin['a_phone'],'user'=>$input['phone'],'details'=>$input['content']);
            $data = $sms->send_verify($admin['a_phone'], json_encode ( $smsParams ));
            if ($data['Code'] == 'OK' && $data['Message'] == 'OK') {
                create_file("./uploads/log_push.txt", "",$admin['a_phone'].'/发送成功/'.$date);
            } else {
                create_file("./uploads/log_push.txt", "",$admin['a_phone'].'/发送失败/'.$date);
            }
        }
        return messageInfo ( '200', '留言成功！' );
    }
}
